package service

import (
	"code/project12/customerManage/model"
)
type CustomerService struct{

	//customers 成员变量是个切片【索引数组】，数组的元素数据类型为Customer结构体
	customers []model.Customer
	customerNum int 
}

func NewCustomerService()*CustomerService{

	customerService:=&CustomerService{}
	customerService.customerNum=1

	customer:= model.NewCustomer(1,"jack","male",20,"18896871476","33732523@qq.com")

	//customerService.customers[0]=customer
	customerService.customers = append(customerService.customers,customer);

	return customerService
}

//CustomerService 结构体的成员方法
func (this *CustomerService) List() []model.Customer{

	return this.customers
}
//CustomerService 结构体的成员方法
func (this *CustomerService) Add(customer model.Customer) bool{

	this.customerNum++
	customer.Id=this.customerNum
	this.customers = append(this.customers,customer)
	return true
}
func (this *CustomerService) DeleteById(id int ) bool{

	index:=this.FindById(id)

	if index==-1{
		return false 
	}

	this.customers = append(this.customers[:index],this.customers[index+1:]...)

	return true
}
func (this *CustomerService) FindById(id int ) int{

	index:=-1
	for i:=0;i<len(this.customers);i++{
		if this.customers[i].Id==id{
			index=i 
			break 
		}
	}
	return index
}